package com.knoya.chejian.pda.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.minterface.MainButtonInterface
import com.knoya.chejian.pda.module.chache.ChaCheActivity
import com.knoya.chejian.pda.module.chuku.ChukuActivity
import com.knoya.chejian.pda.module.outsourcing.OutsourcingActivity
import com.knoya.chejian.pda.module.pandelete.PanDeleteActivity
import com.knoya.chejian.pda.module.pandian.PandianActivity
import com.knoya.chejian.pda.module.ruku.RukuActivity
import com.knoya.chejian.pda.module.shezhi.ShezhiActivity
import com.knoya.chejian.pda.module.yuzupan.yuzupanActivity
import com.knoya.chejian.pda.module.zupan.ZupanActivity
import com.knoya.chejian.pda.module.zupanchaxun.ZupanChaxunActivity
import com.knoya.chejian.pda.myadapter.MainButtonAdapter
import com.knoya.chejian.pda.myclass.UpdataInfo
import com.qs.wiget.PrintUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.header_knoya.*
import kotlin.concurrent.thread
import org.xutils.x.app
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Build
import com.knoya.chejian.pda.module.packageTurnover.PackageTurnoverActivity
import com.knoya.chejian.pda.myclass.QuanXian
import com.knoya.chejian.pda.util.Myutil
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception


class MainActivity : BaseActivity(),MainButtonInterface {


    lateinit var listbutton: MutableList<String>
    lateinit var gvMainButtonAdapter : MainButtonAdapter
    override fun getLayoutId(): Int {
        when(jiemian){
            1 -> return R.layout.activity_main
            2 -> return R.layout.activity_main_2
        }
        return R.layout.activity_main
    }

    override fun initCreate() {
        tv_header_title.text = "选择操作"
        listbutton = ArrayList()
        if(QuanXian.rucang){
            listbutton.add("入库")
            listbutton.add("出库")
            listbutton.add("调仓")
            listbutton.add("组盘查询")
            listbutton.add("组盘")
            listbutton.add("预组盘")
            listbutton.add("删除组盘")
        }

        if(QuanXian.waixie){
            listbutton.add("外协扫描")
        }
        if(QuanXian.rucang){
            listbutton.add("包盘点")
            listbutton.add("叉车采集")
            listbutton.add("叉车采集")
        }

        if(QuanXian.package_turnover){
            listbutton.add("周转")
        }
        listbutton.add("设置")
        gvMainButtonAdapter = MainButtonAdapter(listbutton,this,this)
        GvMainButton.adapter = gvMainButtonAdapter
        UpdataInfo(this, true, sp).show()
        Myutil.MyTost(Build.MODEL)
        if(Build.MODEL == "5501L"){
            PrintUtils.initPrintUtils(this)
            thread(start = true,isDaemon = false){
                Thread.sleep(2000L)
                val bt2 = byteArrayOf(0x1F, 0x1B, 0x1F, 0x80.toByte(), 0x04, 0x05, 0x06, 0x44)
                //发送命令
                PrintUtils.send(bt2)
            }
        }


    }

    override fun onbutton(data: String) {
        when(data){
            "入库" -> {
                val intent = Intent(this@MainActivity, RukuActivity::class.java)
                intent.putExtra("code",1)
                startActivity(intent)
            }
            "出库" -> {
                val intent = Intent(this@MainActivity, ChukuActivity::class.java)
                startActivity(intent)
            }
            "调仓" -> {
                val intent = Intent(this@MainActivity, RukuActivity::class.java)
                intent.putExtra("code",2)
                startActivity(intent)
            }
            "组盘查询" -> {
                val intent = Intent(this@MainActivity, ZupanChaxunActivity::class.java)
                startActivity(intent)
            }
            "组盘" -> {
                val intent = Intent(this@MainActivity, ZupanActivity::class.java)
                startActivity(intent)
            }
            "预组盘" -> {
                val intent = Intent(this@MainActivity, yuzupanActivity::class.java)
                startActivity(intent)
            }
            "删除组盘" -> {
                val intent = Intent(this@MainActivity, PanDeleteActivity::class.java)
                startActivity(intent)
            }
            "外协扫描" -> {
                val intent = Intent(this@MainActivity, OutsourcingActivity::class.java)
                startActivity(intent)
            }
            "包盘点" -> {
                val intent = Intent(this@MainActivity, PandianActivity::class.java)
                startActivity(intent)
            }
            "叉车采集" -> {
                val intent = Intent(this@MainActivity, ChaCheActivity::class.java)
                startActivity(intent)
            }
            "周转" -> {
                val intent = Intent(this@MainActivity, PackageTurnoverActivity::class.java)
                startActivity(intent)
            }
            "设置" -> {
                val intent = Intent(this@MainActivity, ShezhiActivity::class.java)
                startActivity(intent)
            }
        }
    }

}
